<?php
require_once 'config/database.php';

echo "<h2>数据库连接检查</h2>";

// 检查数据库连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "✅ 数据库连接成功<br>";

// 检查数据库是否存在
$sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" . DB_NAME . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    echo "✅ 数据库 " . DB_NAME . " 存在<br>";
} else {
    echo "❌ 数据库 " . DB_NAME . " 不存在<br>";
}

// 检查表是否存在
$tables = array('users', 'production_details');
foreach ($tables as $table) {
    $sql = "SHOW TABLES LIKE '$table'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo "✅ 表 $table 存在<br>";
        
        // 检查表中的记录数
        $sql = "SELECT COUNT(*) as count FROM $table";
        $result = $conn->query($sql);
        $count = $result->fetch_assoc()['count'];
        echo "  - 包含 $count 条记录<br>";
    } else {
        echo "❌ 表 $table 不存在<br>";
    }
}

// 显示MySQL错误日志
echo "<br>最后一次MySQL错误: " . $conn->error; 